home *** CD-ROM | disk | FTP | other *** search
/ Apple Developer Connection Student Program / ADC Tools Sampler CD Disk 3 1999.iso / Metrowerks CodeWarrior / Java Support / Java_Source / IFC_112 / netscape / util / ClassInfo.java < prev    next >
Encoding:
Text File  |  1999-05-28  |  5.9 KB  |  215 lines  |  [TEXT/CWIE]

  1. // ClassInfo.java
  2. // By Ned Etcode
  3. // Copyright 1995, 1996, 1997 Netscape Communications Corp.  All rights reserved.
  4.  
  5. package netscape.util;
  6.  
  7. /** Object subclass describing the schema for a class' encoding. When a Codable
  8.   * object's <b>describeClassInfo()</b> method is invoked, the Codable object
  9.   * must populate the ClassInfo instance with its class name, version, field
  10.   * names, and field types:
  11.   *
  12.   * <pre>
  13.   *     public void describeClassInfo(ClassInfo info) {
  14.   *         super.describeClassInfo(info);
  15.   *
  16.   *         info.addClass("MyView", 1);
  17.   *         info.addField("someObject", OBJECT_TYPE);
  18.   *         info.addField("someString", STRING_TYPE);
  19.   *         info.addField("someInt", INT_TYPE);
  20.   *     }
  21.   * </pre>
  22.   *
  23.   * Each class in the class hierarchy supplies information only about itself.
  24.   * All subclasses should call <b>super.describeClassInfo()</b> to allow each
  25.   * superclass to add additional class name/class version pairs and field
  26.   * name/field type pairs.
  27.   * @note 1.0 Removed builder specific properties, thereby removing dependency
  28.   *           upon the BuilderInfo object
  29.   */
  30. public class ClassInfo {
  31.     String className;
  32.  
  33.     int classCount;
  34.     String classNames[];
  35.     int classVersions[];
  36.  
  37.     int fieldCount;
  38.     String fieldNames[];
  39.     byte fieldTypes[];
  40.  
  41.     // We need to store the most derived class because it needs to be
  42.     // preserved, and may not be in the inheritance path.
  43.  
  44.     /** Constructs a ClassInfo instance for the class named <b>className</b>.
  45.       * The class' name is the most derived class of the object for which the
  46.       * ClassInfo is being built.
  47.       */
  48.     public ClassInfo(String className) {
  49.         super();
  50.         this.className = className;
  51.         classNames = new String[8];
  52.         classVersions = new int[8];
  53.         fieldNames = new String[24];
  54.         fieldTypes = new byte[24];
  55.     }
  56.  
  57.     /** Adds an additional class name/class version pair. This information
  58.       * can be used during decoding to bring forward old encodings at run
  59.       * time.
  60.       * @see Decoder#getVersion
  61.       */
  62.     public void addClass(String className, int version) {
  63.         ensureClassCapacity(classCount);
  64.         classNames[classCount] = className;
  65.         classVersions[classCount] = version;
  66.         classCount++;
  67.     }
  68.  
  69.     /** Adds a field name/field type pair.
  70.       */
  71.     public void addField(String fieldName, byte fieldType) {
  72.         ensureFieldCapacity(fieldCount);
  73.         fieldNames[fieldCount] = fieldName;
  74.         fieldTypes[fieldCount] = fieldType;
  75.         fieldCount++;
  76.     }
  77.  
  78.     /** Returns the name of the most derived class for this ClassInfo.
  79.       */
  80.     public String className() {
  81.         return className;
  82.     }
  83.  
  84.     /** Returns the number of classes which have been added with
  85.       * <b>addClass()</b>.
  86.       * @see #addClass
  87.       */
  88.     public int classCount() {
  89.         return classCount;
  90.     }
  91.  
  92.     /** Returns an array of the class names added with <b>addClass()</b>.
  93.       * @see #addClass
  94.       */
  95.     public String[] classNames() {
  96.         String tmp[];
  97.  
  98.         tmp = new String[classCount];
  99.         System.arraycopy(classNames, 0, tmp, 0, classCount);
  100.  
  101.         return tmp;
  102.     }
  103.  
  104.     /** Returns an array parallel to <b>classNames()</b> with corresponding
  105.       * versions added with <b>addClass()</b>.
  106.       * @see #classNames
  107.       * @see #addClass
  108.       */
  109.     public int[] classVersions() {
  110.         int tmp[];
  111.  
  112.         tmp = new int[classCount];
  113.         System.arraycopy(classVersions, 0, tmp, 0, classCount);
  114.  
  115.         return tmp;
  116.     }
  117.  
  118.     /** Returns the number of fields added with <b>addField()</b>.
  119.       * @see #addField
  120.       */
  121.     public int fieldCount() {
  122.         return fieldCount;
  123.     }
  124.  
  125.     /** Returns an array of all the field names added with <b>addField()</b>.
  126.       * @see #addField
  127.       */
  128.     public String[] fieldNames() {
  129.         String tmp[];
  130.  
  131.         tmp = new String[fieldCount];
  132.         System.arraycopy(fieldNames, 0, tmp, 0, fieldCount);
  133.  
  134.         return tmp;
  135.     }
  136.  
  137.     /** Returns an array parallel to <b>fieldNames()</b> of all the field types
  138.       * added with <b>addField()</b>.
  139.       * @see #fieldNames
  140.       * @see #addField
  141.       */
  142.     public byte[] fieldTypes() {
  143.         byte tmp[];
  144.  
  145.         tmp = new byte[fieldCount];
  146.         System.arraycopy(fieldTypes, 0, tmp, 0, fieldCount);
  147.  
  148.         return tmp;
  149.     }
  150.  
  151.     protected void ensureClassCapacity(int cap) {
  152.         int newCap, oldLen;
  153.         String newInheritancePath[];
  154.         int newClassVersions[];
  155.  
  156.         if (cap < classNames.length)
  157.             return;
  158.  
  159.         if (classNames.length == 0)
  160.             newCap = 8;
  161.         else
  162.             newCap = 2 * classNames.length;
  163.  
  164.         while (newCap < cap)
  165.             newCap = 2 * newCap;
  166.  
  167.         oldLen = classNames.length;
  168.         newInheritancePath = new String[newCap];
  169.         newClassVersions = new int[newCap];
  170.  
  171.         System.arraycopy(classNames, 0, newInheritancePath, 0, oldLen);
  172.         System.arraycopy(classVersions, 0, newClassVersions, 0, oldLen);
  173.  
  174.         classNames = newInheritancePath;
  175.         classVersions = newClassVersions;
  176.     }
  177.  
  178.     protected void ensureFieldCapacity(int cap) {
  179.         String  newFieldNames[];
  180.         int     newCap, oldLen;
  181.         byte    newFieldTypes[];
  182.  
  183.         newCap = fieldCapacityFor(cap);
  184.         if(newCap < 0)
  185.             return;
  186.  
  187.         oldLen = fieldNames.length;
  188.         newFieldNames = new String[newCap];
  189.         newFieldTypes = new byte[newCap];
  190.  
  191.         System.arraycopy(fieldNames, 0, newFieldNames, 0, oldLen);
  192.         System.arraycopy(fieldTypes, 0, newFieldTypes, 0, oldLen);
  193.  
  194.         fieldNames = newFieldNames;
  195.         fieldTypes = newFieldTypes;
  196.     }
  197.  
  198.     public int fieldCapacityFor(int cap)    {
  199.         int newCap = 0;
  200.  
  201.         if (cap < fieldNames.length)
  202.             return -1;
  203.  
  204.         if (fieldNames.length == 0)
  205.             newCap = 24;
  206.         else
  207.             newCap = 2 * fieldNames.length;
  208.  
  209.         while (newCap < cap)
  210.             newCap = 2 * newCap;
  211.  
  212.         return newCap;
  213.     }
  214. }
  215.